// package: rewords-server-2024
// filename: middleware
// author: diogoxiang@gmail.com
// time: 2024/7/12 下午5:21

package middleware

import (
	"rewords-server-2024/internal/library/ecode"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/util/gmeta"

	"rewords-server-2024/internal/consts"
	"rewords-server-2024/internal/library/response"
	"rewords-server-2024/utility/charset"
	"rewords-server-2024/utility/simple"
)

// ResponseHandler HTTP响应预处理
func (s *sMiddleware) ResponseHandler(r *ghttp.Request) {
	r.Middleware.Next()

	// 错误状态码接管
	switch r.Response.Status {
	case 403:
		r.Response.Writeln("403 - 网站拒绝显示此网页")
		return
	case 404:
		r.Response.Writeln("404 - 你似乎来到了没有知识存在的荒原…")
		return
	}

	contentType := getContentType(r)
	// 已存在响应
	if contentType != consts.HTTPContentTypeStream && r.Response.BufferLength() > 0 { // && contexts.Get(r.Context()).Response != nil
		return
	}

	switch contentType {
	case consts.HTTPContentTypeHtml:
		s.responseHtml(r)
		return
	case consts.HTTPContentTypeXml:
		s.responseXml(r)
		return
	case consts.HTTPContentTypeStream:
	default:
		responseJson(r)
	}
}

// responseHtml html模板响应
func (s *sMiddleware) responseHtml(r *ghttp.Request) {
	code, message, resp := parseResponse(r)
	if code == ecode.OK.Code() {
		return
	}

	r.Response.ClearBuffer()
	_ = r.Response.WriteTplContent(simple.DefaultErrorTplContent(r.Context()), g.Map{"code": code, "message": message, "stack": resp})
}

// responseXml xml响应
func (s *sMiddleware) responseXml(r *ghttp.Request) {
	code, message, data := parseResponse(r)
	response.RXml(r, code, message, data)
}

// responseJson json响应
func responseJson(r *ghttp.Request) {
	code, message, data := parseResponse(r)
	response.RJson(r, code, message, data)
}

// parseResponse 解析响应数据
func parseResponse(r *ghttp.Request) (code int, message string, resp interface{}) {
	ctx := r.Context()
	err := r.GetError()
	handlerResponse := r.GetHandlerResponse()

	// 检查返回值是否已经是自定义格式
	if customResp, ok := handlerResponse.(g.Map); ok {
		if _, exists := customResp["status"]; exists {
			// 如果返回值包含 status 字段，认为是自定义格式，直接返回
			return ecode.OK.Code(), "ok", handlerResponse
		}
	}

	// 处理错误情况
	if err != nil {
		if simple.Debug(ctx) {
			message = gerror.Current(err).Error()
			if getContentType(r) == consts.HTTPContentTypeHtml {
				resp = charset.SerializeStack(err)
			} else {
				resp = charset.ParseErrStack(err)
			}
		} else {
			message = consts.ErrorMessage(gerror.Current(err))
		}

		code = gerror.Code(err).Code()

		// 记录异常日志
		if code == gcode.CodeNil.Code() {
			g.Log().Stdout(false).Infof(ctx, "exception:%v", err)
		} else {
			g.Log().Errorf(ctx, "exception:%v", err)
		}
		return
	}

	// 默认成功响应
	return ecode.OK.Code(), "ok", handlerResponse
}

func getContentType(r *ghttp.Request) (contentType string) {
	contentType = r.Response.Header().Get("Content-Type")
	if contentType != "" {
		return
	}

	mime := gmeta.Get(r.GetHandlerResponse(), "mime").String()
	if mime == "" {
		contentType = consts.HTTPContentTypeJson
	} else {
		contentType = mime
	}
	return
}
